<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use Mail;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Crypt;
class resetController extends Controller
{
    public function forget()
    {
    	return view('admin.reset.forget');
    }
    
    public function sendEmail(Request $request)
    {
      $data = $request -> except('_token');
      $res = DB::table('users')
           -> where('email',$data['email'])
           ->first();
      if(!$res)
      {
        return back() -> with(['您输入的邮箱不存在']);
      }	
       
      //dd($data);
      // Mail::raw('这是一封来自于PHP168的邮件', function ($message) use($data) {
      //      $message -> subject('PHP168班欢迎您');
      //      $message -> to($data['email']);
      // });
      Mail::send('admin.reset.resetPass',['res' => $res],function($message) use($data){
      	   $message -> subject('PHP168班欢迎您');
      	   $message -> to($data['email']);
           
      });

    }
    //重置密码表
    public function newPassword($id ,$token)
    {
      //查询
      $res = DB::table('users')
          -> where('id',$id)
          -> where('remember_token',$token)
          -> first();
          //dd($res);
      if(!$res)
      {
      	return view('admin.login.login',['info' => '不是合法的来源']);
      }else{
        return view('admin.reset.setPassword',['id' => $id]);
      }
    }
    public function setpass(Request $request)
    {
      $data = $request -> all();
      
      if($data['password'] != $data['repassword'])
      {
      	return back() -> with(['info' => '两次密码输入不一致']);
      }
         $this -> validate($request,[
        	  'password' => 'required',
        	  'password' => 'min:6'
        	],[
        	  'password.required' => '密码不能为空',
        	  'password.min' => '最少要输入6位密码'
        	]);
          $id = $data['id'];
         // dd($id);
          $data = $request -> except('_token','id','repassword');
          //密码加密
          $data['password'] = Crypt::encrypt($data['password']);
         
           $res = DB::table('users')
               -> where('id',$id)
               -> update($data);
          if($res)
          {
          	return redirect('/admin/login/login') -> with(['info' => '设置成功']);
          }else{
          	return back() -> with(['info' => '设置失败']);
          }   
    }	
}
